import { fetchReviewsList } from '../../../services/reviews/fetchReviewsList';

Page({
  data: {
    reviewsList: [],
    keyword: '',
    layout: 0,
    hasLoaded: false,
    loadMoreStatus: 0,
    loading: true,
    groupId: 0,
  },

  pageNum: 1,
  pageSize: 10,
  total: 0,

  generalQueryData(reset = false) {
    const { keyword } = this.data;
    const { pageNum, pageSize } = this;

    const params = {
      user_id: wx.getStorageSync('user_id'),
      group_id: '',
      page_num: pageNum,
      page_size: pageSize,
      is_hot: false,
      keyword: keyword,
    };

    if (reset) return params;
    return {
      ...params,
      pageNum: pageNum + 1,
      pageSize,
    };
  },

  async init(reset = true, classId, queryKeyword) {
    const { loadMoreStatus } = this.data;
    const params = this.generalQueryData(reset);

    if (!!queryKeyword) {
      params.keyword = queryKeyword.toString().trim();
    } else {
      params.keyword = '';
    }

    if (!!classId) {
      params.group_id = classId.toString();
    }

    if (loadMoreStatus !== 0) return;
    this.setData({
      loadMoreStatus: 1,
      loading: true,
    });
    try {
      const nextList = await fetchReviewsList(params).then((data) => {
        return data.review_list.map((item) => {
          return {
            spuId: item.reviews_id,
            thumb: item.primary_image,
            title: item.reviews_title,
            avatar: item.reviews_avatar,
            author: item.reviews_author,
            mark: item.mark,
            issue: item.release_time,
            hot: item.is_hot,
            liked: item.is_liked,
            likerNum: item.liker_num,
          };
        });
      });

      if (nextList.length > 0) {
        const totalCount = nextList.length;
        const _loadMoreStatus = nextList.length === totalCount ? 2 : 0;
        this.pageNum = params.pageNum || 1;
        this.total = totalCount;
        this.setData({
          reviewsList: reset ? nextList : this.data.reviewsList.concat(nextList),
          loadMoreStatus: _loadMoreStatus,
        });
      } else {
        this.total = nextList.length;
        this.setData({
          emptyInfo: {
            tip: '抱歉，未找到相关内容',
          },
          hasLoaded: true,
          loadMoreStatus: 0,
          loading: false,
          reviewsList: [],
        });
        return;
      }
    } catch (error) {
      this.setData({
        loading: false,
      });
    }
    this.setData({
      hasLoaded: true,
      loading: false,
    });
  },

  onLoad(query) {
    const { groupId, keyword } = query;
    console.log(groupId, '::query value::');
    this.setData({
      keyword,
      groupId,
    });
    this.init(true, groupId, keyword);
  },

  onReachBottom() {
    const { reviewsList, groupId, keyword } = this.data;
    const { total = 0 } = this;
    if (reviewsList.length === total) {
      this.setData({
        loadMoreStatus: 2,
      });
      return;
    }
    this.init(false, groupId, keyword);
  },

  showFilterPopup() {
    this.setData({
      show: true,
    });
  },

  showFilterPopupClose() {
    this.setData({
      show: false,
    });
  },

  gotoGoodsDetail(e) {
    const { reviews } = e.detail;

    wx.navigateTo({
      url: `/pages/reviews/details/index?spuId=${reviews.spuId}`,
    });
  },
});
